package org.vectrics.common.view;

import java.util.Properties;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.vectrics.common.view.StandardPage;
import org.apache.log4j.Category;

abstract public class PageBuilder {
    private static Category log = Category.getInstance(PageBuilder.class);
    private Properties invalidParams = new Properties();
    private HttpServletRequest request = null;
    private Properties params = new Properties();


    abstract protected void doBuild(HttpServletRequest request, StandardPage page);

    public void clear() {
        log.debug("clear() called");
        invalidParams.clear();
    }



    protected String getParam(HttpServletRequest request, String paramName) {
        String value = request.getParameter(paramName);
        if (value == null) {
            value = params.getProperty(paramName);
            if (value == null)
                value ="";
        }
        return(value);
    }

    // SHow all params (passed and list from 'params' data member.
    public void logParams(Category logCat, HttpServletRequest request) {
        if (logCat.isDebugEnabled()) {
            StringBuffer buf = new StringBuffer();
            buf.append("Request parameters:\n");

            Enumeration paramEnum = request.getParameterNames();
            while (paramEnum.hasMoreElements()) {
                String name = (String)paramEnum.nextElement();
                String value = request.getParameter(name);
                buf.append(" - request param value: " + name + " - " + value + "\n");
            }

            buf.append("Additional parameters:\n");
            paramEnum = params.keys();
            while (paramEnum.hasMoreElements()) {
                String name = (String)paramEnum.nextElement();
                String value = params.getProperty(name);
                buf.append(" - request param value: " + name + " - " + value + "\n");
            }
            logCat.debug(buf);
        }

    }


    public void addParam(String name, String value) {
        params.setProperty(name, value);
    }


    public void setInvalidParams(Properties invalid) {
        Enumeration invalidEnums = invalid.keys();
        while (invalidEnums.hasMoreElements()) {
            String name = (String)invalidEnums.nextElement();
            log.debug("Adding invalid param: " + name);
            addInvalidParam(name, invalid.getProperty(name));
        }
    }

    protected void addInvalidParam(String paramName, String description) {
        invalidParams.setProperty(paramName, description);
        if (log.isDebugEnabled()) {
            log.debug("addInvalidParam() " + paramName + " : " + description);
        }
    }

    public String getInvalidParam(String paramName) {
        String desc = invalidParams.getProperty(paramName);
        if (log.isDebugEnabled()) {
            log.debug("getInvalidParam(" + paramName + ") = " + desc);
        }
        return(desc);
    }

    public Properties getInvalidParams() {
        return(invalidParams);
    }


    public void build(HttpServletRequest request, StandardPage page) {
        log.debug("build() called");
        this.request = request;
        doBuild(request, page);
    }

    public void build(HttpServletRequest request, StandardPage page, Properties invalidParams) {
        log.debug("build(request, page, invalidParams) called");
        setInvalidParams(invalidParams);
        this.request = request;
        doBuild(request, page);
    }


    // THis method should be overriden by the derived class if validation is needed.
    public boolean validate(HttpServletRequest request, StandardPage page) {

        boolean valid = true;
        invalidParams.clear();
        doValidate(request);
        if (invalidParams.size() > 0) {
            valid = false;
            log.debug("validate() - bad entry found - INVALID");
        }
        else {
            log.debug("validate() - OK");
        }
        return(valid);
    }

    protected void doValidate(HttpServletRequest request) {
    }


    protected String getValue(String paramName) {
        return(getValue(paramName, request));
    }

    protected int getValue(String paramName, int defaultValue) {
        return(getValue(paramName, request, defaultValue));
    }

    protected long getValue(String paramName, long defaultValue) {
        return(getValue(paramName, request, defaultValue));
    }

    protected String getValue(String paramName, HttpServletRequest request) {
        String value = "";
        if (request != null) {
            if (request.getParameter(paramName) != null) {
                value = request.getParameter(paramName);
            }
        }
        return(value);
    }


    protected int getValue(String paramName, HttpServletRequest request, int defaultValue) {
        int intValue = defaultValue;
        String value = "";
        if (request.getParameter(paramName) != null) {
            value = request.getParameter(paramName);
        }

        if (value.length() > 0) {
            intValue = Integer.parseInt(value);
        }
        return(intValue);
    }

    protected long getValue(String paramName, HttpServletRequest request, long defaultValue) {
        long longValue = defaultValue;
        String value = "";
        if (request.getParameter(paramName) != null) {
            value = request.getParameter(paramName);
        }

        if (value.length() > 0) {
            longValue = Long.parseLong(value);
        }
        return(longValue);
    }

}